package com.bosssoft.cloud.wechat.common.infra.util;


import com.bosssoft.cloud.wechat.common.bean.user.UserInfoDTO;
import com.bosssoft.cloud.wechat.infrastructure.enums.ErrEnum;
import com.bosssoft.cloud.wechat.infrastructure.util.Assert;

import javax.servlet.http.HttpSession;

/**
 * 用户session工具类
 *
 * @author wcy create on 2021/7/19 12:05 下午
 */
public class UserSessionUtil {
    private final static int SESSION_MAX_INACTIVE_INTERVAL = 30 * 60;// 30分

    // 用户信息在session中key值
    private final static String LOGIN_SESSION_KEY = "userinfo";

    public static void setUserInfo(HttpSession session, UserInfoDTO dto) {
        if (dto == null) {
            session.removeAttribute(LOGIN_SESSION_KEY);
            return;
        }
        session.setMaxInactiveInterval(SESSION_MAX_INACTIVE_INTERVAL);
        session.setAttribute(LOGIN_SESSION_KEY, dto);
    }

    public static UserInfoDTO getUserInfo(HttpSession session) {
        UserInfoDTO userInfo = (UserInfoDTO) session.getAttribute(LOGIN_SESSION_KEY);
        Assert.notNull(userInfo, ErrEnum.A0231);
        return userInfo;
    }
}
